package com.itany.netclass.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.netclass.constant.PageConstant;
import com.itany.netclass.constant.ResponseCodeConstant;
import com.itany.netclass.entity.Resource;
import com.itany.netclass.entity.SearchForm;
import com.itany.netclass.exception.NoQuanXianException;
import com.itany.netclass.exception.NullSringException;
import com.itany.netclass.exception.PDFException;
import com.itany.netclass.exception.ServiceException;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.ResourceService;
import com.itany.netclass.util.ParameterUtil;
import com.itany.netclass.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * Author:jiyinchao@itany.com
 * Date:2021/10/17 14:13
 * Version:1.0-SNAPSHOT
 */
@RequestMapping("/resource")
public class ResourceController {


    @RequestMapping("/uploadPDF")
    public  void uploadPDF(HttpServletRequest request,HttpServletResponse response) {
        String chapterId = request.getParameter("chapterId");
        System.out.println("chapterId=" + chapterId);
        ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceService");
        try {
           resourceService.uploadPDF( chapterId,request.getSession(),response.getOutputStream());

        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (NullSringException e) {
            e.printStackTrace();
        } catch (PDFException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
      }


      @ResponseBody
      @RequestMapping("/findMp4ByChapterId")
      public ResponseResult findMp4ByChapterId(HttpServletRequest request, HttpServletResponse response){
          ResponseResult result=new ResponseResult();
          String chapterId = request.getParameter("chapterId");
          System.out.println("chapterId=" + chapterId);
          String backbiaozhi = request.getParameter("backbiaozhi");
          System.out.println("backbiaozhi=" + backbiaozhi);
          ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceService");
          try {
              request.getSession().setAttribute("backbiaozhi",backbiaozhi);
              Resource resource = resourceService.findMp4ByChapterId(chapterId);
              request.getSession().setAttribute("showResource", resource);
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
              result.setReturnObject(resource);
          } catch (PDFException e) {
              e.printStackTrace();
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_NOQUANXIAN);
              result.setMessage(e.getMessage());
          } catch (NullSringException e) {
              e.printStackTrace();
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
              result.setMessage(e.getMessage());
          } catch (ServiceException e) {
              e.printStackTrace();
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
              result.setMessage(e.getMessage());
          }
          return result;
      }
      @RequestMapping("/findResourceById")
     public String findResourceById(HttpServletRequest request,HttpServletResponse response){
          String resourceId = request.getParameter("resourceId");
          request.getSession().setAttribute("resourceSearchForm",new SearchForm());
          ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceService");
          String pageNo = request.getParameter("pageNo");
          String pageSize = request.getParameter("pageSize");
          if(ParameterUtil.isNull(pageNo)){
              pageNo= PageConstant.PAGE_KAISHI_NO;
          }
          if(ParameterUtil.isNull(pageSize)){
              pageSize=PageConstant.PAGE_SIZE;
          }
          try {
              PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
              List<Resource> list=resourceService.findResourceById(resourceId);
              PageInfo pageInfo =new PageInfo(list);
              request.getSession().setAttribute("tishi","上述为您刚刚查看的资源详情，点击查询可以查看所有资源");
              request.getSession().setAttribute("resourceList",pageInfo);
//              request.getSession().setAttribute("backbiaozhi","0");
              return "/showResource.do";
          } catch (ServiceException e) {
              e.printStackTrace();
              return "/showMP4.do";
          }
      }
      @RequestMapping("/findResourceByForm")
    public  String findResourceByForm(HttpServletRequest request,HttpServletResponse response){
          String title = request.getParameter("title");
          System.out.println("title="+title);
          String nickname = request.getParameter("nickname");
          System.out.println("nickname="+nickname);
          String kaishitime = request.getParameter("kaishitime");
          System.out.println("kaishitime="+kaishitime);
          String jieshutime = request.getParameter("jieshutime");
          System.out.println("jieshutime="+jieshutime);
          String status = request.getParameter("status");
          System.out.println("status="+status);
          String fileType = request.getParameter("fileType");
          System.out.println("fileType="+fileType);
          request.getSession().setAttribute("qianyiye",null);
          request.getSession().setAttribute("backbiaozhi","0");


//          request.getSession().setAttribute("kaishitime",kaishitime);
//          request.getSession().setAttribute("title",title);
//          request.getSession().setAttribute("jieshutime",jieshutime);
//          request.getSession().setAttribute("status",status);
//          request.getSession().setAttribute("fileType",fileType);
//          request.getSession().setAttribute("nickname",nickname);
          SearchForm searchForm = new SearchForm();
          searchForm.setTitle(title);
          searchForm.setUserNickname(nickname);
          searchForm.setKaishitime(kaishitime);
          searchForm.setJiesutime(jieshutime);
          searchForm.setStatus(status);
          searchForm.setFileType(fileType);
          request.getSession().setAttribute("resourceSearchForm",searchForm);
          ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceService");
          String pageNo = request.getParameter("pageNo");
          String pageSize = request.getParameter("pageSize");
          if(ParameterUtil.isNull(pageNo)){
              pageNo= PageConstant.PAGE_KAISHI_NO;
          }
          if(ParameterUtil.isNull(pageSize)){
              pageSize=PageConstant.PAGE_SIZE;
          }

          try {
              PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
              List<Resource> list=resourceService.findResourceByForm(title,nickname,kaishitime,jieshutime,status,fileType);
              PageInfo pageInfo =new PageInfo(list);
              request.getSession().setAttribute("tishi",null);
              request.getSession().setAttribute("resourceList",pageInfo);
              request.getSession().setAttribute("Mrg",null);
              return "/showResource.do";
          } catch (NullSringException e) {
              e.printStackTrace();
              request.getSession().setAttribute("Mrg",e.getMessage());
              request.getSession().setAttribute("resourceList",new PageInfo<Resource>());
              return "/showResource.do";
          } catch (ServiceException e) {
              e.printStackTrace();
              return "/showResource.do";
          }

      }
      @RequestMapping("/findAllResource")
    public String findAllResource(HttpServletRequest request,HttpServletResponse response){
//          request.getSession().setAttribute("kaishitime",null);
//          request.getSession().setAttribute("title",null);
//          request.getSession().setAttribute("jieshutime",null);
//          request.getSession().setAttribute("status",null);
//          request.getSession().setAttribute("fileType",null);
//          request.getSession().setAttribute("nickname",null);
//          request.getSession().setAttribute("Mrg",null);
//          request.getSession().setAttribute("tishi",null);
          request.getSession().setAttribute("resourceSearchForm",new SearchForm());
          request.getSession().setAttribute("resourceList",new PageInfo<Resource>());
          request.getSession().setAttribute("qianyiye",null);
          request.getSession().setAttribute("backbiaozhi","0");
          request.getSession().setAttribute("tishi",null);



          ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceService");
          String pageNo = request.getParameter("pageNo");
          String pageSize = request.getParameter("pageSize");
          if(ParameterUtil.isNull(pageNo)){
              pageNo= PageConstant.PAGE_KAISHI_NO;
          }
          if(ParameterUtil.isNull(pageSize)){
              pageSize=PageConstant.PAGE_SIZE;
          }
          List<Resource> list= null;
          try {
              PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
              list = resourceService.findAllResource();
              PageInfo pageInfo =new PageInfo(list);
              request.getSession().setAttribute("resourceList",pageInfo);
              return "/showResource.do";
          } catch (NullSringException e) {
              e.printStackTrace();
              request.getSession().setAttribute("Mrg",e.getMessage());
              return "/showResource.do";
          } catch (ServiceException e) {
              e.printStackTrace();
              return "/showResource.do";

          }

      }
      @RequestMapping("/findResourceByForm2")
    public String findResourceByForm2(HttpServletRequest request,HttpServletResponse response){
          SearchForm searchForm= (SearchForm) request.getSession().getAttribute("resourceSearchForm");
          String kaishitime = searchForm.getKaishitime();
          String title= searchForm.getTitle();
          String jieshutime = searchForm.getJiesutime();
          String status = searchForm.getStatus();
          String  fileType = searchForm.getFileType();
          String nickname = searchForm.getUserNickname();
          request.getSession().setAttribute("backbiaozhi","0");
          request.getSession().setAttribute("tishi",null);

          ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceService");
          String pageNo=null;//响应后显示的页数
          String pageNo1 = request.getParameter("pageNo");//get传过来的页数

          //pageNo2在第一次查询所有时没有意义，作用是用来保证通过别的按钮返回当前页时留在当前页
          PageInfo<Resource> Info = (PageInfo<Resource>) request.getSession().getAttribute("resourceList");
          String pageNo2="";
          if(Info!=null){
              pageNo2=""+Info.getPageNum();
          }

          //这个是表示在找到结果是如果数据改了，导致页面变少，自动跳转到前一页的页数
          String pageNo3 = (String) request.getSession().getAttribute("qianyiye");
          System.out.println("pageNo1="+pageNo1);
          System.out.println("pageNo2="+pageNo2);
          System.out.println("pageNo3="+pageNo3);


          //以下为使用哪一个作为当前页数的判断
          if(pageNo3!=null){
              System.out.println("用了前一页");
              pageNo=pageNo3;
          }else if(pageNo1!=null){
              System.out.println("用了get");
              pageNo=pageNo1;
          }else if(!ParameterUtil.isNull(pageNo2)){
              System.out.println("用了session");
              pageNo=pageNo2;
          }else {
              System.out.println("用了常量");
              pageNo=PageConstant.PAGE_KAISHI_NO;
          }

          String pageSize = request.getParameter("pageSize");
          if(pageSize==null){
              pageSize=PageConstant.PAGE_SIZE;
          }
          List<Resource> list= null;
          try {
              PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
              list=resourceService.findResourceByForm(title,nickname,kaishitime,jieshutime,status,fileType);
              PageInfo pageInfo =new PageInfo(list);
              request.getSession().setAttribute("resourceList",pageInfo);
              request.getSession().setAttribute("qianyiye", null);
              request.getSession().setAttribute("Mrg",null);
              return "/showResource.do";
          } catch (NullSringException e) {
              if(pageNo.equals(PageConstant.PAGE_KAISHI_NO)){
                  //已经是第一页，表示为无满足条件的对象
                  request.getSession().setAttribute("resourceList",new PageInfo<>());
                  request.getSession().setAttribute("Mrg","未查询到任何结果");
                  return "/showResource.do";
              }else {
                  //设置前一页的页码放到session中
                  String pageNum = ""+(Info.getPageNum()-1);
                  request.getSession().setAttribute("qianyiye",pageNum);
                  return "/resource/findResourceByForm2.do";
              }
          } catch (ServiceException e) {
              e.printStackTrace();
              return "/showResource.do";

          }
      }
      @ResponseBody
      @RequestMapping("/yongResource")
      public  ResponseResult yongResource(HttpServletRequest request,HttpServletResponse response){
          ResponseResult result=new ResponseResult();
          String resourceId = request.getParameter("resourceId");
          String status = request.getParameter("status");
          ResourceService resourceService= (ResourceService) ObjectFactory.getObject("ResourceService");
          try {
              resourceService.yongResource(resourceId,status);
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
          } catch (NoQuanXianException e) {
              e.printStackTrace();
              result.setMessage(e.getMessage());
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
          } catch (ServiceException e) {
              e.printStackTrace();
              result.setMessage(e.getMessage());
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
          } catch (Exception e) {
              e.printStackTrace();
              result.setMessage(e.getMessage());
              result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
          }
          return result;
      }

    }
